package com.xj.qq;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.appcompat.app.AppCompatActivity;

import com.xj.qq.swipe.ShapeView;

public class Loading extends AppCompatActivity {
    private ShapeView mShapeView;
    private View mShadowView;
    boolean isStopAnimation = false;
    float mTranslateDistance =80.0f;
    long mAnimateTime = 500;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_layout);
        initLayout();
    }

    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            Intent intent = new Intent(Loading.this, MainActivity.class);
            startActivity(intent);
        }
        return true;
    }

    /**
     *加载布局，初始化控件
     */
    private void initLayout() {
        mShapeView = findViewById(R.id.shapeview);
        mShadowView = findViewById(R.id.shadowview);
        //开启动画
        mShadowView.post(new Runnable() {
            @Override
            public void run() {
                startDropAnimation();
            }
        });
    }
    /**
     * 定义 下落动画 和 阴影缩小动画
     */
    private void startDropAnimation() {
        if (isStopAnimation) {
            return;
        }
        //下落的动画
        ObjectAnimator mTranslateAnimation = ObjectAnimator.ofFloat(mShapeView, "translationY", 0, mTranslateDistance);
        mTranslateAnimation.setInterpolator(new AccelerateInterpolator());
        //阴影缩小动画
        ObjectAnimator mScaleAnimation = ObjectAnimator.ofFloat(mShadowView, "scaleX", 1f, 0.4f);
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(mTranslateAnimation, mScaleAnimation);
        mAnimatorSet.setDuration(mAnimateTime);
        mAnimatorSet.setInterpolator(new AccelerateInterpolator());
        mAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mShapeView.changeShape();
                startUpAnimation();
            }
        });
        mAnimatorSet.start();
    }
    /**
     * 定义 弹起动画 和 阴影放大动画
     */
    private void startUpAnimation() {
        if (isStopAnimation) {
            return;
        }
        //弹起的动画
        ObjectAnimator mTranslateAnimation = ObjectAnimator.ofFloat(mShapeView, "translationY", mTranslateDistance, 0);
        //阴影放大的动画
        ObjectAnimator mScaleAnimation = ObjectAnimator.ofFloat(mShadowView, "scaleX", 0.4f, 1f);
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(mTranslateAnimation, mScaleAnimation);
        mAnimatorSet.setDuration(mAnimateTime);
        mAnimatorSet.setInterpolator(new DecelerateInterpolator());

        mAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                //弹起时旋转
                startRotateAnimation();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                startDropAnimation();
            }
        });
        mAnimatorSet.start();

    }
    //旋转动画
    private void startRotateAnimation() {
        if (isStopAnimation) {
            return;
        }
        ObjectAnimator mRotateAnimation = null;
        switch (mShapeView.getCurrentShape()) {
            case CIRCLE:
            case RECTANGLE:
                mRotateAnimation = ObjectAnimator.ofFloat(mShapeView, "rotation", 0, 180);
                break;
            case TRIANGLE:
                mRotateAnimation = ObjectAnimator.ofFloat(mShapeView, "rotation", 0, -120);
                break;
        }
        mRotateAnimation.setDuration(mAnimateTime);
        mRotateAnimation.start();
    }
}

